home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / text / NumberFormat.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  8.7 KB  |  288 lines

  1. /****i* SOURCE_FILE/INFO
  2.   *
  3.   * NAME
  4.   *  NumberFormat.js
  5.   *
  6.   * USAGE
  7.   *  Part of Netobjects JavaScript Library.
  8.   *
  9.   * COPYRIGHT
  10.   *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.   *  All Rights Reserved.
  12.   *
  13.   *  This is an unpublished work protected by Website Pros, Inc.
  14.   *  as a trade secret, and is not to be used or disclosed except as
  15.   *  expressly provided in a written license agreement executed by
  16.   *  you and Website Pros, Inc.
  17.   *
  18.   *      <copyright@websitepros.com>
  19.   *
  20.   * NOTES
  21.   *  JavaScript code.
  22.   *
  23.   *****/
  24. if (!IS.isModuleInitialized("IS.NOF.TEXT.NumberFormat"))
  25.     
  26.   /****h* NOF_JavaScript_Library/NOF.TEXT.NumberFormat
  27.     *
  28.     * NAME
  29.     *  NOF.TEXT.NumberFormat
  30.     *
  31.     * DESCRIPTION
  32.     *
  33.     * <code>NumberFormat</code> is a class which provides a simple way to 
  34.     * format a number using locale specific separators.
  35.     * Usage sample:
  36.     * 
  37.     *   var myLocale = new NOF.UTIL.Locale("de", "DE");
  38.     *   var nf = new NOF.TEXT.NumberFormat(myLocale, "#,##0.##");
  39.     *   var str = nf.format(-13003.4567);
  40.     *   alert( str );
  41.     *
  42.     * External dependencies: NOF.UTIL.Locale, NOF.UTIL.DefaultLocale, NOF.UTIL.ResourceBundle, NOF.UTIL.Exception
  43.     ****/
  44.     
  45.   /**
  46.     * Constructor
  47.     **/  
  48.   function TEXT_NumberFormat(/*NOF.UTIL.Locale*/ locale, /*String*/ pattern) {
  49.         this.__proto__ = TEXT_NumberFormat.prototype;
  50.         
  51.         this.locale        = locale;
  52.         this.pattern    = pattern;                
  53.         
  54.         this.NumberFormatSymbols = null;
  55.         
  56.     }
  57.     {
  58.         var method = TEXT_NumberFormat.prototype;
  59.         
  60.         method.SYMBOLS_RESOURCE = "lib/nof/text/NumberFormatSettings";
  61.         
  62.         method.TEXT_NumberFormatSymbols =    function () {
  63.             this.decimalSeparator = arguments[0];
  64.             this.groupingSeparator = arguments[1];
  65.             this.patternSeparator = arguments[2];
  66.             this.minusSign = arguments[3];
  67.             this.percent = arguments[4];            
  68.             this.zeroDigit = arguments[5]; //see Arabic
  69.             this.digit = arguments[6];            
  70.             this.exponential = arguments[7];
  71.             this.perMill = arguments[8];
  72.             this.infinity  = arguments[9];
  73.             this.NN = arguments[10];    
  74.             this.defaultPattern = arguments[11];
  75.         }
  76.         
  77.         method.getNumberFormatSymbols = function(/*NOF.UTIL.Locale*/ locale) {
  78.             if (locale == null) {
  79.                 locale = this.locale;
  80.             }
  81.             var propSymbols = NOF.UTIL.ResourceBundle.getBundle(this.SYMBOLS_RESOURCE, locale);            
  82.             if (propSymbols == null) {
  83.                 return null;
  84.             } else {
  85.                 return new this.TEXT_NumberFormatSymbols(
  86.                     propSymbols.getProperty("decimalSeparator"),
  87.                     propSymbols.getProperty("groupingSeparator"),
  88.                     propSymbols.getProperty("patternSeparator"),
  89.                     propSymbols.getProperty("minusSign"),
  90.                     propSymbols.getProperty("percent"),
  91.                     propSymbols.getProperty("zeroDigit"),
  92.                     propSymbols.getProperty("digit"),            
  93.                     propSymbols.getProperty("exponential"),
  94.                     propSymbols.getProperty("perMill"),
  95.                     propSymbols.getProperty("infinity"),
  96.                     propSymbols.getProperty("NaN"),
  97.                     propSymbols.getProperty("defaultPattern")
  98.                     );                            
  99.             }
  100.         }
  101.         
  102.         /**
  103.         * Set locale        
  104.         * @param locale - specific locale         
  105.         **/
  106.         method.setLocale = function (/*NOF.UTIL.Locale*/ locale) {
  107.             if ( (locale != null) && (this.locale.equals(locale) == false) ) {
  108.                 this.locale = locale;
  109.                 this.NumberFormatSymbols = this.getNumberFormatSymbols(locale);
  110.             }
  111.             
  112.         }
  113.         /**
  114.         * Get locale        
  115.         * @return locale
  116.         **/
  117.         /*NOF.UTIL.Locale*/ method.getLocale = function () {
  118.             return this.locale;
  119.         }
  120.         
  121.         /**
  122.         * Set pattern        
  123.         * @param pattern 
  124.         **/
  125.         method.setPattern = function (/*String*/ pattern) {
  126.             if ( (pattern != null) && (this.pattern != pattern) ) {
  127.                 this.pattern = pattern;                
  128.             }            
  129.         }
  130.         /**
  131.         * Get pattern
  132.         * @return pattern
  133.         **/
  134.         /*String*/ method.getPattern = function () {
  135.             return this.pattern;
  136.         }
  137.         
  138.         /**
  139.         * Format a number.
  140.         * @param n the number
  141.         * @param locale (optional) - specific locale. Does not change current locale. 
  142.         * @return the string obtained by formating the number n 
  143.         **/
  144.         /*String*/ method.format = function (/*Number*/ n, /*NOF.UTIL.Locale*/ locale) {
  145.             if (n == null) return null;            
  146.             var nfs = null;
  147.             if (locale != null) {
  148.                 nfs = this.getNumberFormatSymbols(locale);
  149.             } else {    //no locale argument                        
  150.                 if (this.NumberFormatSymbols == null) { //first call
  151.                     locale = (this.locale != null) ? this.locale : NOF.UTIL.DefaultLocale;
  152.                     this.NumberFormatSymbols = this.getNumberFormatSymbols(locale);
  153.                 }                
  154.                 nfs = this.NumberFormatSymbols;
  155.             }
  156.             //alert("nfs.decimalSeparator = " + nfs.decimalSeparator);
  157.             var str = "" + n;
  158.             
  159.             var isNegative = (str.charAt(0) == '-');
  160.             var minusAtBegining = true;
  161.             
  162.             var pattern = this.pattern;
  163.             if (pattern == null || pattern.trim().length == 0) {
  164.                 pattern = nfs.defaultPattern;
  165.             } 
  166.             if (pattern.indexOf(nfs.patternSeparator) > -1) {
  167.                 if (isNegative == true) {                    
  168.                     pattern = pattern.substring(pattern.indexOf(nfs.patternSeparator) + 1, pattern.length);
  169.                     var iMinusInPattern = pattern.indexOf("-"); //should be > -1
  170.                     minusAtBegining = ( iMinusInPattern < pattern.indexOf(nfs.digit) );
  171.                     pattern = pattern.substring(0, iMinusInPattern) + pattern.substring(iMinusInPattern + 1, pattern.length);
  172.                     
  173.                 } else {
  174.                     pattern = pattern.substring(0, pattern.indexOf(nfs.patternSeparator));                
  175.                 }
  176.             }
  177.             
  178.             if (isNegative == true) {
  179.                 str = str.substring(1, str.length);
  180.             }
  181.             
  182.             var in_intPart = "" + parseInt(str);                            
  183.             var in_floatPart = "0";
  184.             if (str.indexOf(".") > -1) {
  185.                 in_floatPart = str.substring(str.indexOf(".") + 1, str.length);
  186.             }
  187.             
  188.             //#,##0.##        eval("1.E"+1)
  189.             var out_intPart, out_floatPart;
  190.             
  191.             var i = pattern.indexOf(/*nfs.decimalSeparator*/ ".");
  192.             var floatPartLength = 0;
  193.             if (i > -1) {
  194.                 //floatPartLength = pattern.length - (i + 1); 
  195.                 floatPartLength = pattern.lastIndexOf(nfs.digit) - i;
  196.             }            
  197.             out_floatPart = "" + Math.round( eval("1." + in_floatPart + "E" + floatPartLength) );
  198.             out_floatPart = out_floatPart.substring(1, out_floatPart.length);        
  199.             //alert("pattern " + pattern + " floatPartLength = "+floatPartLength);
  200.             
  201.             var groupLength = (i > -1) ? (i - pattern.lastIndexOf(/*nfs.groupingSeparator*/ ",") - 1): 3;
  202.             //alert("groupLength " + groupLength);
  203.             
  204.             var out_intPartArr = [];
  205.             while(in_intPart.length > 0) {
  206.                 i = in_intPart.length - groupLength;
  207.                 if (i > 0) {
  208.                     tmpStr = in_intPart.substring(i, in_intPart.length);
  209.                     in_intPart = in_intPart.substring(0, i);
  210.                 } else {
  211.                     tmpStr = in_intPart;
  212.                     in_intPart = "";
  213.                 }                
  214.                 
  215.                 out_intPartArr[out_intPartArr.length] = tmpStr;                                
  216.             }
  217.             
  218.             out_intPart = out_intPartArr.reverse().join(nfs.groupingSeparator);
  219.             
  220.             str = (out_intPart + nfs.decimalSeparator + out_floatPart);
  221.             if (isNegative == true) {
  222.                 str = (minusAtBegining == true) ? (nfs.minusSign + str) : (str + nfs.minusSign);
  223.             }
  224.             
  225.             return str;                            
  226.         }
  227.         
  228.         /**
  229.         * get a number from a formatted string
  230.         * @param source
  231.         * @param locale (optional) - specific locale. Does not change current locale. 
  232.         * @return the number (as a float)
  233.         **/        
  234.         /*Number*/ method.parse    = function (/*String*/ source, /*NOF.UTIL.Locale*/ locale) {
  235.             if (source == null || source.trim().length == 0) {
  236.                 return null;    //should throw exception        
  237.             }
  238.             
  239.             var nfs = null;
  240.             if (locale != null) {
  241.                 nfs = this.getNumberFormatSymbols(locale);
  242.             } else {    //no locale argument                        
  243.                 if (this.NumberFormatSymbols == null) { //first call
  244.                     locale = (this.locale != null) ? this.locale : NOF.UTIL.DefaultLocale;
  245.                     this.NumberFormatSymbols = this.getNumberFormatSymbols(locale);
  246.                 }                
  247.                 nfs = this.NumberFormatSymbols;
  248.             }
  249.             var i = source.indexOf(nfs.minusSign);
  250.             var iDec = source.indexOf(nfs.decimalSeparator);
  251.             var minusAtBegining = true;
  252.             
  253.             var isNegative = (i > -1);
  254.             if (isNegative == true) {
  255.                 source = source.substring(0, i) + source.substring(i+1, source.length);                    
  256.                 if (i > iDec) { 
  257.                     minusAtBegining = false;                    
  258.                 } else {
  259.                     iDec--;
  260.                 }
  261.             }
  262.             
  263.             var intPart = "", floatPart = "0";
  264.             if (iDec == -1) {
  265.                 intPart = source;
  266.             } else {
  267.                 intPart = source.substring(0, iDec);
  268.                 floatPart = source.substring(iDec + 1, source.length);
  269.             }
  270.             
  271.             //var reGroup = /[nfs.groupingSeparator]/;
  272.             //while(intPart.indexOf(nfs.groupingSeparator) > 0) {
  273.             //    intPart = intPart.replace(reGroup, "");
  274.             //}
  275.             intPart = (intPart.split(nfs.groupingSeparator)).join("");
  276.             
  277.             var n = parseFloat(intPart + "." + floatPart);
  278.             
  279.             if (isNegative == true) {
  280.                 n *= -1;
  281.             }
  282.             return n;
  283.         }
  284.     }
  285.     // add it to NOF.TEXT namespace    
  286.     TEXT.__proto__.NumberFormat = TEXT_NumberFormat;
  287. }